/**
* @Title: ScreenUtil.java
* @Description: ${TODO} 类描述
* @Copyright 方正国际软件有限公司,Inc.Allrightsreserved.
 * @author Founder 
* @date 2011-7-25 12:51:23
* @version V1.0 
 */
package com.founder.android;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

public class PlayerUtil {
	
	public static final String TYPE_IMAGE = "image";
	
	public static final String TYPE_VIDEO = "video";
	
	public static final String TYPE_TXT = "txt";
	
	public static boolean hasSDCard() {
		return Environment.MEDIA_MOUNTED.equals(
				Environment.getExternalStorageState());
	}
	
	public static String getPath(String dir) {
		if (!hasSDCard()) {
			Log.e("PlayerUtil", "SD Card not found!");
		}
		return Environment.getExternalStorageDirectory() 
				+ "/adfile/" + dir;
	}

	public static List<File> filterDirFile(String path, String type) {
		List<File> files = new ArrayList<File>();
		File root = new File(path);
        if (root.exists()){
            File[] fileNames = root.listFiles();
        	for (int i = 0; i < fileNames.length; i++) {
        		//文件类型过滤
        		if (isValidType(fileNames[i], type)){
        			files.add(fileNames[i]);
        		}
        	}
        }
    	return files;
	}
	
	//建立一个MIME类型与文件后缀名的匹配表
	private static Map<String, String> mimeMap = new HashMap<String, String>();
	
	static {
		mimeMap.put(".png",	TYPE_IMAGE);
		mimeMap.put(".bmp",	TYPE_IMAGE);
		mimeMap.put(".gif", TYPE_IMAGE);
		mimeMap.put(".jpeg", TYPE_IMAGE);
		mimeMap.put(".jpg",	TYPE_IMAGE);
		mimeMap.put(".mp4",	TYPE_VIDEO);
		mimeMap.put(".3gp",	TYPE_VIDEO);
		mimeMap.put(".avi",	TYPE_VIDEO);
		mimeMap.put(".txt",	TYPE_TXT);
	}
	
	/*
	 * 判断文件类型是否合法
	 */
	private static boolean isValidType(File file, String type) {
		String fName=file.getName();
		//获取后缀名前的分隔符"."在fName中的位置。
		int dotIndex = fName.lastIndexOf(".");
		if (dotIndex < 0) return false;
		/* 获取文件的后缀名 */
		String end = fName.substring(dotIndex, fName.length()).toLowerCase();
		if(end == "") return false;
		//在MIME和文件类型的匹配表中找到对应的MIME类型。
		return type.equals(mimeMap.get(end));
	}

    /*
     * Compute the sample size as a function of minSideLength
     * and maxNumOfPixels.
     * minSideLength is used to specify that minimal width or height of a
     * bitmap.
     * maxNumOfPixels is used to specify the maximal size in pixels that is
     * tolerable in terms of memory usage.
     *
     * The function returns a sample size based on the constraints.
     * Both size and minSideLength can be passed in as IImage.UNCONSTRAINED,
     * which indicates no care of the corresponding constraint.
     * The functions prefers returning a sample size that
     * generates a smaller bitmap, unless minSideLength = IImage.UNCONSTRAINED.
     *
     * Also, the function rounds up the sample size to a power of 2 or multiple
     * of 8 because BitmapFactory only honors sample size this way.
     * For example, BitmapFactory downsamples an image by 2 even though the
     * request is 3. So we round up the sample size to avoid OOM.
     */
	public static int computeSampleSize(BitmapFactory.Options options,
            int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8 ) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 :
                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 :
                (int) Math.min(Math.floor(w / minSideLength),
                Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) &&
                (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
 
}